package com.linglan.modules.demo.controller;


import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.json.JSONUtil;
import com.linglan.common.api.R;
import com.linglan.common.api.ResultCode;
import com.linglan.common.utils.*;
import com.linglan.modules.demo.entity.Demo;
import com.linglan.modules.demo.service.DemoService;
import com.linglan.modules.demo.vo.DemoVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * (Demo)表控制层
 *
 * @author makejava
 * @since 2023-08-10 22:01:55
 */
@RestController
@RequestMapping("/demo/demo")
@Slf4j
public class DemoController {
    /**
     * 服务对象
     */
    @Resource
    private DemoService demoService;
    @Resource
    private RedisUtil redisUtil;


    /**
     * 国际化 校验参数
     * @param demoVO
     * @return
     */
    @GetMapping(value = "getI18nValidate")
    @SaIgnore
    public R<String> getI18nValidate(@RequestBody DemoVO demoVO) {
        ValidationResult validationResult = LingLanValidator.validateEntity(demoVO);
        System.out.println(JSONUtil.toJsonStr(validationResult));
        if (BooleanUtil.isTrue(validationResult.getHasErrors())) {
            Map<String, String> errorMsg = validationResult.getErrorMsg();
            String value = errorMsg.entrySet().stream().findFirst().get().getValue();
            return R.failed(value);
        }
        return R.ok("success");
    }

    /**
     * 国际化 基本信息
     * 入参必须携带lang 接口无需使用，默认会选择对应的语言解析，未找到则用中文
     * @param lang
     * @return
     */
    @GetMapping(value = "/getI18n")
    public R<String> getI18n(@RequestParam(name = "lang", required = false)String lang) {
        log.info("{}", I18nMessageUtil.get("err.sys"));
        return R.ok(I18nMessageUtil.get("err.sys"));

    }

    /**
     * 全局异常消息国际化
     *
     * @return
     */
    @GetMapping(value = "globalExceptionMsgI18n")
    public R<String> globalExceptionMsgI18n() {
        ExceptionUtil.throwException(ResultCode.SITE_ID_NOT_EMPTY.getMessageKey());
        return R.ok("success");
    }


    @GetMapping(value = "/get")
    public R<DemoVO> get(@RequestParam(name = "id") Long id) {
        Demo byId = demoService.getById(id);
        log.info("{}", byId.getWord());
        DemoVO vo = new DemoVO();
        BeanUtils.copyProperties(byId, vo);
        return R.ok(vo);
    }

    @GetMapping(value = "/getCache")
    public R<DemoVO> getCache() {
        Demo byId = demoService.getById(2L);
        log.info("{}", byId.getWord());
        DemoVO vo = new DemoVO();
        BeanUtils.copyProperties(byId, vo);
        String s = JSONUtil.toJsonStr(vo);
        redisUtil.set("demovo1", s, 10000);
        String demovo1 = redisUtil.get("demovo1");
        System.out.println(demovo1);
        DemoVO demoVO = JSONUtil.toBean(demovo1, DemoVO.class);
        return R.ok(demoVO);
    }

    @PostMapping(value = "/post")
    public String post(@RequestParam(name = "word")String word) {
        Demo demo = new Demo();
        demo.setId(2L);
        demo.setWord(word);
        demoService.save(demo);
        return "succ";
    }

    @PutMapping(value = "/put")
    public boolean put(@RequestParam(name = "id") Long id) {
        return demoService.removeById(id);
    }
}

